﻿using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace NodeEditorFramework.SkillComposer
{
    public class DataType
    {
        public const string String = "System.String";
        public const string Float = "Float";
        public const string Texture = "Texture";
        public const string Format = "SkillFormat";
    }

    [Node(false, "Skill/skill_index")]
    public class SkillIndexNode : Node
    {
        public const string ID = "SkillIndexNode";
        public override string GetID { get { return ID; } }
        public override bool AutoLayout { get { return true; } } // IMPORTANT -> Automatically resize to fit list
        public override Vector2 MinSize { get { return new Vector2(200, 10); } }

        private List<List<string>> confList = new List<List<string>>
        {
            new List<string>{ "skill_up_id", "技能信息id", DataType.String },
            new List<string>{ "skill_info_id", "技能id", DataType.String },
            new List<string>{ "skill_name_id", "技能名称文案 ID", DataType.String },
            new List<string>{ "battle_text", "战斗飘字", DataType.String },
            new List<string>{ "skill_level", "技能等级", DataType.Float },
            new List<string>{ "open_class", "技能开放阶级", DataType.Float },
            new List<string>{ "skill_quality", "技能品质", DataType.Float },
            new List<string>{ "open_star", "技能开放等级", DataType.Float },
            new List<string>{ "open_level", "技能开放等级", DataType.Float },
            new List<string>{ "sing", "吟唱类型与时间", DataType.Float },
            new List<string>{ "sing_text", "吟唱文本", DataType.String },
            new List<string>{ "comsum", "消耗", DataType.Format },
            new List<string>{ "skill_add", "额外技能", DataType.Format },
            new List<string>{ "rouse_skillchange", "觉醒替换", DataType.String },
            new List<string>{ "skill_replace", "技能替换", DataType.Format },
            new List<string>{ "skill_replace_condition", "技能替换条件", DataType.Format },
            new List<string>{ "active_trigger", "主动技能拖放方式", DataType.Format },
            new List<string>{ "add_trigger", "附加型技能附加方式", DataType.Format },
            new List<string>{ "atk_distance", "攻击距离", DataType.Float },
            new List<string>{ "atk_num", "攻击次数", DataType.Float },
            new List<string>{ "func1_trigger", "触发概率", DataType.Float },
            new List<string>{ "skill_trigger", "技能触发方式", DataType.Format },
            new List<string>{ "cd", "冷却时间", DataType.Float },
            new List<string>{ "skill_intro_id", "技能功能描述文案id", DataType.String },
            new List<string>{ "skill_intro_in_id", "技能功能局内描述文案id", DataType.String },
            new List<string>{ "func_frame", "技能功能", DataType.Format },
            new List<string>{ "skill_effect_intro", "技能特殊效果描述", DataType.String },
            new List<string>{ "skill_effect_intro_id", "技能特殊效果文案id", DataType.String },
            new List<string>{ "duration", "持续时间", DataType.Float },
            new List<string>{ "pursue_area", "技能追击距离", DataType.Float },
            new List<string>{ "skill_force", "技能力度", DataType.Float },
            new List<string>{ "skill_weight", "技能附加重量", DataType.Float },
        };

        protected override void OnCreate()
        {
            for (int i = 0; i < confList.Count; i++)
            {
                var data = confList[i];
                string confId = data[0];
                string confTitle = data[1];
                string dataType = data[2];

                var dynaCreationAttribute = new ValueConnectionKnobAttribute(confId, Direction.In, dataType);
                CreateValueConnectionKnob(dynaCreationAttribute);
            }
        }

        public override void NodeGUI()
        {
            for (int i = 0; i < confList.Count; i++)
            {
                var data = confList[i];
                string confId = data[0];
                string confTitle = data[1];
                string dataType = data[2];

                GUILayout.BeginHorizontal();
                GUILayout.Label(confId);
                GUILayout.Label(confTitle);
                GUILayout.EndHorizontal();
                ((ValueConnectionKnob)dynamicConnectionPorts[i]).SetPosition();
            }

            if (GUILayout.Button("OutData", GUILayout.ExpandWidth(false)))
            {
                var data = OutData();
                string jsonString = JsonConvert.SerializeObject(data);
                Debug.Log(jsonString);
            }
        }

        public override bool Calculate()
        {
            return true;
        }

        public override object OutData()
        {
            var data = new Dictionary<string, object>();

            for (int i = 0; i < confList.Count; i++)
            {
                string key = confList[i][0];
                if (dynamicConnectionPorts[i].connected())
                {
                    SkillIndexNode.AddConnection(data, key, dynamicConnectionPorts[i] as ValueConnectionKnob);
                }
            }

            return data;
        }

        public static void AddConnection(Dictionary<string, object> data, string key, ValueConnectionKnob item)
        {
            foreach (var connection in item.connections)
            {
                data[key] = connection.body.OutData();
            }
        }
    }
}